﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SStuff.FlowControls
{
    public class CoverFlow3D : IFlowLayout3DAlgorithm
    {
        Size _availableSize;

        #region IFlowLayout3DAlgorithm Members

        public void SetAvailableSize(Size availableSize)
        {
            _availableSize = availableSize;
        }

        public int GetMaxDisplayedItems()
        {
            return 11;
        }

        CircleEase _circleOutEase = new CircleEase { EasingMode = EasingMode.EaseOut };
        CubicEase _cubicOutEase = new CubicEase { EasingMode = EasingMode.EaseOut };

        public void ComputeLayout(double normalizedOffset, IFlowItem3D item, int consideredItemsCount)
        {
            double step = 1.0 / (double)consideredItemsCount;

            var absOffset = Math.Abs(normalizedOffset);

            if (absOffset < step)
            {
                var closeOffset = normalizedOffset * consideredItemsCount;
                item.Transform3D.GlobalOffsetX = closeOffset * 0.15 * _availableSize.Width;
                item.Transform3D.GlobalOffsetZ = -Math.Abs(closeOffset) * 250;
                item.Transform3D.RotationY = closeOffset * 60;
                item.Opacity = 1;
            }
            else
            {
                item.Transform3D.GlobalOffsetX = normalizedOffset > 0 ? 0.15 * _availableSize.Width : -0.15 * _availableSize.Width;
                item.Transform3D.GlobalOffsetZ = -250;
                var closeOffset = normalizedOffset>0? normalizedOffset - step:normalizedOffset+step;
                item.Transform3D.GlobalOffsetX += 1.35 * closeOffset * _availableSize.Width;
                item.Transform3D.GlobalOffsetZ -= 2000 * Math.Abs(closeOffset);
                item.Transform3D.RotationY = normalizedOffset > 0 ? 60 : -60;

                if (1 - absOffset <= step)
                {
                    var tailOffset = (1 - absOffset) * consideredItemsCount;
                    item.Opacity = tailOffset;
                }
                else
                {
                    item.Opacity = 1;
                }
            }
        }

        public bool SupportsGestures
        {
            get { return true; }
        }

        public double GetNormalizedOffsetAtPoint(Point contactPosition)
        {
            double halfWidth = 0.5 * this._availableSize.Width;
            double x = contactPosition.X - halfWidth;
            return x / halfWidth;
        }

        #endregion
    }
}
